package com.training.admin.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.training.admin.entity.trainee.IdmUser;
import com.training.admin.service.IdmUserService;

/**
 * @author xiaoHU
 *
 */
@Service("idmUserService")
public class IdmUserServieImpl extends DataServiceImpl<IdmUser> implements IdmUserService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /* (non-Javadoc)
     * @see com.training.admin.service.IdmUserService#getUserOrgNameFullPath(java.lang.String)
     */
    public String getUserOrgNameFullPath(String projectLead) {
        IdmUser idmUser = genericDaoSupport.load(IdmUser.class, Long.parseLong(projectLead));

        String itemResult = "";
        
        if (idmUser == null) {
            itemResult = "---";
        }else {
            String item = idmUser.getUserorgnamefullpath();
            String result ="";
            String [] strings = item.split("-");
            for (int i = 2; i<strings.length; i++) {
                if (i < strings.length-1) {
                    result += strings[i] + "-";
                }else {
                    result += strings[i];
                }
            }
            
            if (StringUtils.isEmpty(result)) {
                itemResult = idmUser.getUsercn();
            } else {
                itemResult = idmUser.getUsercn() + "-" + result;
            }
        }
        
        return itemResult;
    }
    
    /*
     * (non-Javadoc)
     * @see com.training.admin.service.IdmUserService#getIdmUser(java.lang.String)
     */
    public IdmUser getIdmUser(String projectLead) {
        IdmUser idmUser = genericDaoSupport.load(IdmUser.class, Long.parseLong(projectLead));
        return idmUser;
    }
    
    /*
     * (non-Javadoc)
     * @see com.training.admin.service.IdmUserService#getIdmUserByEmail(java.lang.String)
     */
    public IdmUser getIdmUserByEmail(String email) {
        IdmUser idmUser = genericDaoSupport.searchForObject("FROM IdmUser WHERE mail = :mail","mail", email, IdmUser.class);
        return idmUser;
    }

}
